﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using WcfServiceLayer.ServiceContracts;
using System.Runtime.Serialization;
using BusinessLogicLayer;
using ModelLayer;
using System.ServiceModel;

namespace WcfServiceLayer.Services
{
    [ServiceBehavior(InstanceContextMode = InstanceContextMode.PerCall, ConcurrencyMode = ConcurrencyMode.Multiple)]
    public class ManagementService : IManagementService
    {
        public bool AddNewBattery(Battery newBattery, string sessionToken)
        {
            AccountManager am = new AccountManager();
            if(!am.CheckEmployeeSessionToken(sessionToken))
            {
                return false;
            }
            BatteryManager batteryCtr = new BatteryManager(); 
            batteryCtr.AddNewBattery(newBattery);
            return true;
        }

        public bool DeleteBattery(Battery battery, string sessionToken)
        {
            AccountManager am = new AccountManager();
            if (!am.CheckEmployeeSessionToken(sessionToken))
            {
                return false;
            }
            BatteryManager batteryCtr = new BatteryManager(); 
            batteryCtr.DeleteBattery(battery);
            return true;
        }

        public bool UpdateBattery(Battery battery, string sessionToken)
        {
            AccountManager am = new AccountManager();
            if (!am.CheckEmployeeSessionToken(sessionToken))
            {
                return false;
            }
            BatteryManager batteryCtr = new BatteryManager(); 
            batteryCtr.UpdateBattery(battery);
            return true;
        }

        public IList<Battery> GetAllBatteries()
        {
            BatteryManager batteryCtr = new BatteryManager(); 
            return batteryCtr.GetAllBatteries();
        }

        public IList<BatteryType> GetAllBatteryTypes()
        {
            ReservationManager rm = new ReservationManager();
            return rm.GetAllBatteryTypes();
        }

        public bool AddNewBatteryType(BatteryType type, string sessionToken)
        {
            AccountManager am = new AccountManager();
            if (!am.CheckEmployeeSessionToken(sessionToken))
            {
                return false;
            }
            BatteryManager bm = new BatteryManager();
            bm.AddNewBatteryType(type);
            return true;
        }

        public bool UpdateBatteryType(BatteryType type, string sessionToken)
        {
            AccountManager am = new AccountManager();
            if (!am.CheckEmployeeSessionToken(sessionToken))
            {
                return false;
            }
            BatteryManager bm = new BatteryManager();
            bm.UpdateBatteryType(type);
            return true;
        }

        public bool DeleteBatteryType(BatteryType type, string sessionToken)
        {
            AccountManager am = new AccountManager();
            if (!am.CheckEmployeeSessionToken(sessionToken))
            {
                return false;
            }
            BatteryManager bm = new BatteryManager();
            bm.DeleteBatteryType(type);
            return true;
        }

        public bool AddStation(Station station, string sessionToken)
        {
            AccountManager am = new AccountManager();
            if (!am.CheckEmployeeSessionToken(sessionToken))
            {
                return false;
            }
            StationManager stationCtr = new StationManager();
            stationCtr.AddNewStation(station);
            return true;
        }

        public bool UpdateStation(Station station, string sessionToken)
        {
            AccountManager am = new AccountManager();
            if (!am.CheckEmployeeSessionToken(sessionToken))
            {
                return false;
            }
            StationManager stationCtr = new StationManager();
            stationCtr.UpdateStation(station);
            return true;
        }

        public bool RemoveStation(Station station, string sessionToken)
        {
            AccountManager am = new AccountManager();
            if (!am.CheckEmployeeSessionToken(sessionToken))
            {
                return false;
            }
            StationManager stationCtr = new StationManager();
            stationCtr.RemoveStation(station);
            return true;
        }

        public IList<Station> GetAllStations()
        {
            StationManager stationCtr = new StationManager();
            return stationCtr.GetAllStations();
        }

        public Station GetStationById(int stationId)
        {
            StationManager stationCtr = new StationManager();
            return stationCtr.GetStationById(stationId);
        }


        public IList<UserAccount> GetAllUserAccounts()
        {
            AccountManager uam = new AccountManager();
            return uam.GetAllUserAccounts();
        }

        public EmployeeAccount LogIn(string username, string password)
        {
            AccountManager am = new AccountManager();
            return am.LogInEmployee(username, password);
        }

        public IList<EmployeeAccount> GetAllEmployeeAccounts()
        {
            AccountManager am = new AccountManager();
            return am.GetAllEmployeeAccounts();
        }

        public bool AddEmployeeAccount(EmployeeAccount account, string sessionToken)
        {
            AccountManager am = new AccountManager();
            if (!am.CheckEmployeeSessionToken(sessionToken))
            {
                return false;
            }
            am.CreateEmployeeAccount(account);
            return true;
        }

        public bool UpdateEmployeeAccount(EmployeeAccount account, string sessionToken)
        {

            AccountManager am = new AccountManager();
            if (!am.CheckEmployeeSessionToken(sessionToken))
            {
                return false;
            }
            am.UpdateEmployeeAccount(account);
            return true;
        }

        public bool DeleteEmployeeAccount(EmployeeAccount account, string sessionToken)
        {
            AccountManager am = new AccountManager();
            if (!am.CheckEmployeeSessionToken(sessionToken))
            {
                return false;
            }
            am.DeleteEmployeeAccount(account);
            return true;
        }


        public int TryToLendBattery(Battery battery, string sessionToken)
        {
            AccountManager am = new AccountManager();
            if (!am.CheckEmployeeSessionToken(sessionToken))
            {
                return -1;
            }
            ReservationManager rm = new ReservationManager();
            return rm.TryToLendBattery(battery) ? 1 : 0;
        }


        public IList<BatteryReservation> GetAllReservations()
        {
            ReservationManager rm = new ReservationManager();
            return rm.GetCurrentReservations();
        }

        public IList<BatteryReservation> GetAllReservationsOnStation(Station station)
        {
            ReservationManager rm = new ReservationManager();
            return rm.GetCurrentReservationsOnStation(station);
        }


        public bool UpdateReservation(BatteryReservation reservation, string sessionToken)
        {
            AccountManager am = new AccountManager();
            if (!am.CheckEmployeeSessionToken(sessionToken))
            {
                return false;
            }
            ReservationManager rm = new ReservationManager();
            rm.UpdateReservation(reservation);
            return true;
        }


        public EmployeeAccount ReloadAccount(string sessionToken)
        {
            AccountManager am = new AccountManager();
            return am.ReloadAccount(sessionToken);
        }
    }
}
